package com.telecom.restaurant.service;

import com.telecom.restaurant.constant.DefaultConstant;
import com.telecom.restaurant.domin.vo.MenuFoodVO;
import com.telecom.restaurant.domin.vo.OrderConfirmVO;
import com.telecom.restaurant.domin.vo.OrderFoodVO;
import com.telecom.restaurant.model.FoodPO;
import com.telecom.restaurant.model.RestaurantPO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

@Component
public class PictureService {

    @Value("#{prop.pic_path}")
    private String pic_path;

    @PostConstruct
    public void renderConstantMain(){
        DefaultConstant.PACKAGE_MAIN_MANTOU.setPicture(this.getPicturePath(DefaultConstant.PACKAGE_MAIN_MANTOU.getPicture()));
        DefaultConstant.PACKAGE_MAIN_RICE.setPicture(this.getPicturePath(DefaultConstant.PACKAGE_MAIN_RICE.getPicture()));

    }


    public String getPicturePath(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        if (name.length() < 30) {
            return pic_path + name;
        }
        return name;
    }

    public void renderFoodPicture(FoodPO foodPO) {
        if (foodPO != null) {
            foodPO.setPicture(this.getPicturePath(foodPO.getPicture()));
        }
    }

    public void renderFoodListPicture(List<FoodPO> foodPOList) {
        if (CollectionUtils.isNotEmpty(foodPOList)) {
            for (FoodPO foodPO : foodPOList) {
                this.renderFoodPicture(foodPO);
            }
        }
    }

    public void renderMenuFoodPicture(List<MenuFoodVO> menuFoodVOList) {
        if (CollectionUtils.isNotEmpty(menuFoodVOList)) {
            for (MenuFoodVO menuFoodVO : menuFoodVOList) {
                menuFoodVO.setPicture(this.getPicturePath(menuFoodVO.getPicture()));
            }
        }
    }

    public void renderOrderFoodVOListPicture(List<? extends OrderFoodVO> orderFoodVOList) {
        if (CollectionUtils.isNotEmpty(orderFoodVOList)) {
            for (OrderFoodVO entity : orderFoodVOList) {
                entity.setPicture(this.getPicturePath(entity.getPicture()));
            }
        }
    }

    public void renderOrderFoodVOPicture(OrderFoodVO orderFood) {
        if (orderFood != null) {
            orderFood.setPicture(this.getPicturePath(orderFood.getPicture()));
        }
    }

    public void renderRestaurantList(List<RestaurantPO> restaurantList) {
        if (CollectionUtils.isNotEmpty(restaurantList)) {
            for (RestaurantPO entity : restaurantList) {
                entity.setPicture(this.getPicturePath(entity.getPicture()));
            }
        }
    }

    public void renderOrderConfirmList(List<OrderConfirmVO> orderConfirmVOList) {
        if (CollectionUtils.isNotEmpty(orderConfirmVOList)) {
            for (OrderConfirmVO entity : orderConfirmVOList) {
                this.renderOrderFoodVOListPicture(entity.getDishList());
            }
        }


    }
}
